﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.DataCaching;
using ZHDJ.Data.Models.BaseInfo;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.Message
{
    public partial class MessageBoardEdit : PageBase
    {

        #region ViewPower
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "MessageBoardEdit";
            }
        }

        #endregion
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }


        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {

            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyMessageBoard current = DB.PartyMessageBoards.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtCreateTime.Text = Convert.ToDateTime(current.CreateTime).ToString("yyyy-MM-dd HH:mm");//留言时间
                txtHandleTime.Text = Convert.ToDateTime(current.HandleTime).ToString("yyyy-MM-dd HH:mm");//回复时间
                txtHandleBy.Text = current.HandleBy;//回复人
                txtHandleContent.Text = current.HandleContent;//回复内容
                txtUserName.Text = current.UserName;//联系人
                txtMessage.Text = current.Message;//留言内容
                cbxMessageState.Checked = current.MessageState == 1;

            }
        }



        private void SaveJobTitle()
        {
            PartyMessageBoard item;
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                //更新
                item = DB.PartyMessageBoards.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
            }
            else
            {
                
                item = new PartyMessageBoard();
                item.ID = StringHelper.NewID;
                DB.PartyMessageBoards.Add(item);
            }
            
            item.MessageState = cbxMessageState.Checked ? 1 : 0;//回复状态
            item.HandleContent = txtHandleContent.Text;//回复内容
            item.UserName = txtUserName.Text;//姓名
            if (txtHandleBy.Text == "")
            {
                item.HandleBy = string.Empty;//回复人
            }
            else {
                item.HandleBy = txtHandleBy.Text;//回复人
            }
            item.Message = txtMessage.Text;//留言内容
            item.HandleTime = DateTime.Now;//回复时间
            
            if (IsLogin)
            {
                item.HandleBy = this.CurrentUser.LoginID;
            }
            else
            {
                item.HandleBy = string.Empty;
            }
            DB.SaveChanges();
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveJobTitle();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}